import { TFace } from "./TFace";
import { LabelActor, PointerActor } from "cax-base";
import { VTActor } from "cax-base";
import { FrontProcessing } from "../index";
export interface TPoint {
  nodeId: number; // 这里的nodeID是格宇软件返回的网格交叉点node
  partId: number; // nodeId 相关联的 partId
  x: number;
  y: number;
  z: number;
}

/**
 * 网格点数据
 * @class TMeshPoint
 * @param { TPoint[] } data
 * @param
 */
export class TMeshPoint extends VTActor {
  data: TPoint[] = [];
  parent: FrontProcessing | undefined;
  constructor(faceData_ins: TFace) {
    super(faceData_ins.polyData);
    this.data = this.faceDataToMeshPointData(faceData_ins);
  }
  /**
   * @name faceDataToMeshPointData 将faceData转换为meshPointData
   * @param {TFace} faceData
   * @return {TPoint[]} meshPointData
   */
  faceDataToMeshPointData(faceData: TFace): TPoint[] {
    const TpointData = faceData.data.flatMap((cell) =>
      cell.nodes.map((node) => ({
        nodeId: node[0],
        partId: cell.partId,
        x: node[1],
        y: node[2],
        z: node[3],
      }))
    );
    function filterAndDeduplicatePoints(points: TPoint[]): TPoint[] {
      const uniquePoints = Array.from(
        new Set(points.map((p) => JSON.stringify(p)))
      ).map((str) => JSON.parse(str));
      return uniquePoints;
    }
    return filterAndDeduplicatePoints(TpointData);
  }
  containsMeshPointId(pointId: number) {
    return this.data.filter((point) => point.nodeId === pointId).length > 0;
  }

  setParent(p: FrontProcessing) {
    this.parent = p;
  }
}
